package com.springfreemwork.tomcat.servlet;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
@author gpo12345678@163.com
@create 2024-04-07 14:37

*/

@Data
public class JsonHttpServlet extends HttpServlet {

	private Object data;


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {

		// 设置内容类型为JSON
		response.setContentType("application/json");
		// 设置字符编码为UTF-8
		response.setCharacterEncoding("UTF-8");
		// 获取PrintWriter对象用于写入响应
		PrintWriter out = response.getWriter();
		// 写入JSON数据
		out.print(JSON.toJSONString(data));
		// 清空缓冲区，发送数据到客户端
		out.flush();
		// 关闭Writer对象
		out.close();
	}
}
